、<template>
  <div class="app-container search-table-box aidex-table">
    <el-card shadow="never" ref="queryRef" style="margin-bottom: 12px;" class="search_card" v-show="showSearch">
      <div class="filter-container">
        <div class="search_box">
          <el-form :model="queryParams" ref="queryForm" label-width="80px">
            <el-row :gutter="16">
              <el-col :md="6" >
                <el-form-item label="公告标题" prop="title">
                  <el-input
                    v-model="queryParams.title"
                    placeholder="请输入公告标题"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :md="6" align="right" style="float: right;">
                <el-form-item>
                  <el-button class="filter-item" type="primary" @click="handleQuery">搜索</el-button>
                  <el-button class="filter-item" style="margin-left: 8px" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
    </el-card>
    <el-card shadow="never" >
      <template #header>
        <el-row>
          <el-col :span="8">
            <div class="card-header">
              <el-button disabled type="text">公告草稿信息</el-button>
            </div>
          </el-col>
          <el-col :span="16">
            <div class="btn_box" align="right" style="float: right;">
              <el-button
                type="primary" 
                icon="el-icon-plus"
                @click="handleAdd"
                v-hasPermi="['system:bulletin:add']"
              >新增</el-button>
              <el-button
                type="success"
                icon="el-icon-position"
                :disabled="multiple"
                @click="handleSend"
                v-hasPermi="['system:bulletin:send']"
              >批量发送</el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['system:bulletin:remove']"
              >批量删除</el-button>
              <right-toolbar :showSearch.sync="showSearch" @showSearchFun="showSearchFun" @queryTable="getList"></right-toolbar>
            </div>
          </el-col>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="bulletinList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公告标题" align="center" prop="title" width="150" show-overflow-tooltip/>
      <el-table-column label="公告内容" align="center" prop="content" width="260" show-overflow-tooltip/>
      <el-table-column label="接收人员" align="center" prop="reciveStaffNames" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"           
            @click="showInfo(scope.row)"
            v-hasPermi="['system:bulletin:info']"
          >查看</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button
            size="mini"
            type="text"
            @click="handleSend(scope.row)"
            v-hasPermi="['system:bulletin:send']"
          >发送</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bulletin:edit']"
          >修改</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:bulletin:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改公告栏对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="720px" top="10vh" append-to-body>
        <el-dialog
        width="900px"
        title="人员选择"
        :visible.sync="innerOpen"
        append-to-body>
        <div class="modal-body" style="height: 450px; padding: 5px;">
          <user-assign v-if="innerOpen" :checkedData="staffData" @saveResult="saveResult"></user-assign>
        </div>
      </el-dialog>
        <div style="height: 530px;overflow: auto; padding: 12px 24px;">
          <el-form ref="form" :model="form" :rules="rules" label-position="top">
            <el-row :gutter="24">
              <el-col :span="24">
                <el-form-item label="公告标题" prop="title">
                  <el-input v-model="form.title" placeholder="请输入公告标题" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="接收人员" prop="receiveStaffNames">
                  <el-input v-model="form.receiveStaffNames" placeholder="请选择接收人员" readonly>
                      <el-button @click="showStaffPanel" slot="append"><i class="el-icon-user"></i>
                      </el-button>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="公告内容">
                  <editor v-model="form.content" :min-height="192"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm('C')">保 存</el-button>
          <el-button type="primary" @click="submitForm('A')">发 送</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </el-card>
     <bulletin-info :info="currInfo" ref="bulletinInfo" :showSendTime="false"></bulletin-info>
  </div>
</template>

<script>
import { listBulletin, getBulletin, delBulletin, addBulletin, updateBulletin, batchSend } from "@/api/system/bulletin";
import UserAssign from '@/components/UserAssign'
import BulletinInfo from './common/bulletinInfo.vue';
export default {
  name: "Bulletin",
  components: {UserAssign, BulletinInfo},
  data() {
    return {
      currInfo: {},
      staffData: [],
      tableHeight: this.getInitTableHeight(),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedBulletinRecive: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告栏表格数据
      bulletinList: [],
      // 公告接收者表格数据
      bulletinReciveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      innerOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sts: "C",
        title: null,
        createUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "公告标题不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "公告内容不能为空", trigger: "blur" }
        ],
        receiveStaffNames: [
          { required: true, message: "接收人员不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    showStaffPanel() {
      this.innerOpen = true
    },
    saveResult(data) {
      // this.staffData = data
      let arr = []
      let arrName = []
      data.forEach(function (row) {
        arr.push(row.userId)
        arrName.push(row.userName)
      })
      this.form.receiveStaffNames = arrName.toString()
      this.form.receiveStaffIds = arr
      this.innerOpen = false
    },
   /** 隐藏搜索按钮操作 */
   showSearchFun(isShowSearch){
      this.showSearch = isShowSearch
      let oldHeight = this.$refs.queryRef.$el.offsetHeight
      if(!isShowSearch){
          //当前是显示状态
          oldHeight = oldHeight + 12
      }else{
          oldHeight = oldHeight - 12
      }
      this.$nextTick(() => (
         this.tableHeight = this.$refs.tableRef.$el.offsetHeight - (this.$refs.queryRef.$el.offsetHeight-oldHeight)
      ))
    },
    /** 查询公告栏列表 */
    getList() {
      this.loading = true;
      listBulletin(this.queryParams).then(response => {
        this.bulletinList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(ex => {
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bulletinId: null,
        title: null,
        content: null,
        receiveStaffIds: [],
        receiveStaffNames: null,
        createTime: null,
        readNum: null,
        sendTime: null,
        createUserId: null,
        updateUserId: null,
        updateTime: null,
        remark: null,
        sts: null
      };
      this.bulletinReciveList = [];
      this.staffData = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bulletinId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.bulletinId = row.bulletinId
      this.title = "修改公告";
      this.open = true
      this.form.title = row.title
      this.form.content = row.content
      this.form.sendTime = row.sendTime
      this.form.receiveStaffIds = row.reciveStaffIds
      this.form.receiveStaffNames = row.reciveStaffNames
      row.bulletinReciveList.forEach(item => {
        this.staffData.push({userId: item.reciveUserId, userName: item.reciveUserName, dept: {deptName: item.reciveDeptName} })
      })
    },
    /** 提交按钮 */
    submitForm(sts) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sts = sts      
          if (this.form.bulletinId != null) {
            updateBulletin(this.form).then(response => {
              this.$modal.msgSuccess(sts === "A" ? "发送成功" : "修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBulletin(this.form).then(response => {
              this.$modal.msgSuccess(sts === "A" ? "发送成功" : "新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    showInfo (row) {
      this.currInfo = row
      this.$refs['bulletinInfo'].show()
    },
    handleSend(row) {
      const bulletinIds = row.bulletinId || this.ids;
      this.$modal.confirm('是否确认发送选中的数据项？').then(function() {
        return batchSend(bulletinIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("发送成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bulletinIds = row.bulletinId || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delBulletin(bulletinIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bulletin/export', {
        ...this.queryParams
      }, `bulletin_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>